﻿using ChoMy.Areas.Admin.Models;
using ChoMy.Models;
using ChoMy.Models.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Areas.Admin.Controllers
{
    public class StatisticsController : Controller
    {
        private ChoMyContext db = new ChoMyContext();

        [Authorize(Roles = "Admin, Statistics Input")]
        public ActionResult Input(string dateRange)
        {
            IQueryable<Product> query = db.Products;

            DateTime toDate = DateTime.Now;
            DateTime fromDate = DateTime.Now.AddDays(-30);

            if (!String.IsNullOrWhiteSpace(dateRange))
            {
                List<DateTime> dateTimeList = dateRange.Split('-').Select(x => DateTime.Parse(x)).ToList();

                fromDate = dateTimeList[0];
                toDate = dateTimeList[1];
            }

            ViewBag.DateRange = fromDate.ToString("dd/MM/yyyy") + " - " + toDate.ToString("dd/MM/yyyy");

            query = query.Where(x => fromDate <= x.DateCreated && x.DateCreated <= toDate);

            List<Input> models = query
                .Select(x => new Input
                {
                    Name = x.CreatedBy,
                    Date = x.DateCreated
                })
                .OrderBy(x => x.Name)
                .ToList();

            return View(models);
        }
    }
}